const { mock } = require('mockjs')
var jglblist = mock({
  'data|4': [
    {
      'id|+1': 1,
      mingcheng: '类别' + '@integer(1, 4)'
    }
  ]
}).data
var data = mock({
  'data|5': [
    {
      'id|+1': 1,
      mingcheng: '测试机构',
      danweinm: 'BM2023',
      shangji: null,
      jigoulb: '@integer(1, 4)'
    },
    {
      'id|+1': 1000,
      mingcheng: '测试机构子集',
      danweinm: 'BM202301',
      shangji: null,
      jigoulb: '@integer(1, 4)'
    },
  ],
}).data
var treelist = mock({
  'data|5': [{
    'id|+1': 1,
    label: '测试数据',
    children: [{
      'id|+1': 1000,
      label: '测试数据子集',
      children: []
    }]
  }]
}).data
module.exports = [
  {
    url: '/jigou/list',
    type: 'post',
    response(config) {
      if (config.body.pageSize) {
        const { title, pageNo, pageSize } = config.body
        let mockList = data.filter((item) => {
          if (title && item.indexOf(title) < 0) return false
          return true
        })
        const pageList = mockList.filter(
          (item, index) =>
            index < pageSize * pageNo && index >= pageSize * (pageNo - 1)
        )
        return {
          code: 200,
          msg: 'success',
          totalCount: mockList.length,
          data: pageList,
        }
      } else {
        return {
          code: 200,
          msg: 'success',
          data: data,
        }
      }
    },
  },
  {
    url: '/jigou/save',
    type: 'post',
    response(config) {
      const body = config.body
      data.push(
        mock({
          id: "@increment",
          danweinm: body.danweinm,
          jigoulb: body.jigoulb,
          mingcheng: body.mingcheng,
          shangji: body.shangji,
          datatime: "@now"
        })
      )
      return {
        code: 200,
        msg: '保存成功',
      }
    },
  },
  {
    url: '/jigou/saveJigouZiyuan',
    type: 'post',
    response() {
      return {
        code: 200,
        msg: '保存成功',
      }
    },
  },
  {
    url: '/jigoulb/query',
    type: 'post',
    response() {
      return {
        code: 200,
        msg: '查询成功',
        data: jglblist
      }
    },
  },
  {
    url: '/jigou/tree',
    type: 'post',
    response() {
      return {
        code: 200,
        msg: '查询成功',
        data: treelist
      }
    },
  },
  {
    url: '/jigou/delete',
    type: 'post',
    response(config) {
      const body = config.body
      const index = data.findIndex((item) => {
        return item.id === body.id
      });
      data.splice(index, 1)
      return {
        code: 200,
        msg: '删除成功',
      }
    },
  },
]
